package com.qk.management.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerPageDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *
 * @author blusr
 * @version 1.0
 * @date 2025/10/16 10:22
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void updateCustomerById(Customer customer) {
        if (ObjectUtil.isEmpty(customer.getPhone()) || ObjectUtil.equals(customer.getPhone(), "") || ObjectUtil.isEmpty(customer.getName()) || ObjectUtil.equals(customer.getName(), "")) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.updateById(customer);
    }

    @Override
    public void addCustomer(CustomerDTO dto) {
        if (ObjectUtil.isEmpty(dto.getPhone()) || ObjectUtil.equals(dto.getPhone(), "") || ObjectUtil.isEmpty(dto.getName()) || ObjectUtil.equals(dto.getName(), "")) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageResult<CustomerPageVO> getCustomerPage(CustomerPageDTO dto) {
        Page<Customer> page = page(new Page<>(dto.getPage(), dto.getPageSize()), Wrappers.lambdaQuery(Customer.class)
                .eq(ObjectUtil.isNotEmpty(dto.getPhone()) && !ObjectUtil.equals(dto.getPhone(), ""), Customer::getPhone, dto.getPhone())
                .eq(ObjectUtil.isNotEmpty(dto.getName()) && !ObjectUtil.equals(dto.getName(), ""), Customer::getName, dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getChannel()) && !ObjectUtil.equals(dto.getChannel(), ""), Customer::getChannel, dto.getChannel())
                .orderByDesc(Customer::getUpdateTime));
        List<CustomerPageVO> list = page.getRecords().stream().map(record -> {
            CustomerPageVO vo = BeanUtil.copyProperties(record, CustomerPageVO.class);
            vo.setCourseName(ObjectUtil.isEmpty(courseMapper.selectById(record.getCourseId())) ? "" : courseMapper.selectById(record.getCourseId()).getName());
            return vo;
        }).collect(Collectors.toList());
        return PageResult.<CustomerPageVO>builder()
                .total(page.getTotal())
                .rows(list)
                .build();
    }
}
